#include <iostream>
#include <fstream>
#include <vector>
#include <string>

struct GridPoint {
    double x, y, z;
};

void readFineGridData(const std::string& filename, std::vector<GridPoint>& gridPoints) {
    std::ifstream file(filename);
    if (!file.is_open()) {
        std::cerr << "无法打开文件: " << filename << std::endl;
        return;
    }
    
    int count = 0; // 计数器，用于统计读取了多少个点
    GridPoint point;
    while (file >> point.x >> point.y >> point.z) { // 假设每行只有x, y, z三个值
        gridPoints.push_back(point);
        ++count;
    }

    std::cout << "从文件 " << filename << " 中读取了 " << count << " 个点" << std::endl;
}

void writeNewFineGridData(const std::string& filename, const std::vector<GridPoint>& newGridPoints) {
    std::ofstream file(filename);
    if (!file.is_open()) {
        std::cerr << "无法创建或写入文件: " << filename << std::endl;
        return;
    }
    
    for (const auto& point : newGridPoints) {
        file << point.x << " " << point.y << " " << point.z << "\n";
    }

    std::cout << "数据已成功写入文件: " << filename << std::endl;
}

std::vector<GridPoint> extendGrid(const std::vector<GridPoint>& originalGrid, int extendX, int extendY, int extendZUp, int extendZDown, double extendXDistance, double extendYDistance, double extendZUpHeight, double extendZDownHeight) {
    std::vector<GridPoint> extendedGrid = originalGrid;

    // X方向延拓
    for (int i = 0; i < extendX; ++i) {
        for (auto& point : extendedGrid) {
            point.x += extendXDistance;
        }
    }

    // Y方向延拓
    for (int i = 0; i < extendY; ++i) {
        for (auto& point : extendedGrid) {
            point.y += extendYDistance;
        }
    }

    // Z方向向上延拓
    for (int i = 0; i < extendZUp; ++i) {
        for (auto& point : extendedGrid) {
            if (point.z > 0) { // 只调整顶面的点
                point.z += extendZUpHeight;
            }
        }
    }

    // Z方向向下延拓
    for (int i = 0; i < extendZDown; ++i) {
        for (auto& point : extendedGrid) {
            if (point.z < 0) { // 只调整底面的点
                point.z -= extendZDownHeight;
            }
        }
    }

    std::cout << "网格延拓完成. 原始点数: " << originalGrid.size() << ", 新点数: " << extendedGrid.size() << std::endl;
    return extendedGrid;
}

int main() {
    std::vector<GridPoint> gridPoints;
    std::string inputFilename = "D:\\360MoveData\\Users\\10758\\Desktop\\yuanzi\\finals\\code\\griddatatrans\\finals\\pcl_geo1\\raw_data\\fine_grid_data.txt";
    readFineGridData(inputFilename, gridPoints);

    if(gridPoints.empty()){
        std::cerr << "未读取到任何点数据." << std::endl;
        return -1;
    }

    // 直接设置延拓参数
    int extendX = 2;         // X方向延拓层数
    int extendY = 2;         // Y方向延拓层数
    int extendZUp = 1;       // Z方向向上延拓层数
    int extendZDown = 1;     // Z方向向下延拓层数
    double extendXDistance = 10.0;   // X方向延拓距离
    double extendYDistance = 10.0;   // Y方向延拓距离
    double extendZUpHeight = 5.0;    // Z方向向上延拓高度
    double extendZDownHeight = 5.0;  // Z方向向下延拓高度

    auto newGridPoints = extendGrid(gridPoints, extendX, extendY, extendZUp, extendZDown, extendXDistance, extendYDistance, extendZUpHeight, extendZDownHeight);

    std::string outputFilename = "new_fine_grid_data.txt";
    writeNewFineGridData(outputFilename, newGridPoints);

    std::cout << "程序执行完成." << std::endl;

    return 0;
}